﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SubjectGroupsViewModel.cs" company="ELEN 7045 GROUP II">>
//   
// </copyright>
// <summary>
//   The SubjectGroups View Model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Presentation.Module.StudyDefinition.ViewModels
{
    #region

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;

    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;

    using Presentation.Module.StudyDefinition.Interfaces.Services;
    using Presentation.Module.StudyDefinition.Interfaces.ViewModels;
    using Presentation.Module.StudyDefinition.Interfaces.Views;
    using Presentation.Module.StudyDefinition.Models;

    #endregion

    /// <summary>
    /// The SubjectGroups View Model.
    /// </summary>
    public class SubjectGroupsViewModel : ISubjectGroupsViewModel, INotifyPropertyChanged
    {
        #region Constants and Fields

        /// <summary>
        /// The region manager instance.
        /// </summary>
        private readonly IRegionManager regionManagerInstance;

        /// <summary>
        /// The subject group add view instance.
        /// </summary>
        private readonly ISubjectGroupAddView subjectGroupAddViewInstance;

        /// <summary>
        /// The subject group edit view instance.
        /// </summary>
        private readonly ISubjectGroupEditView subjectGroupEditViewInstance;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SubjectGroupsViewModel"/> class.
        /// </summary>
        /// <param name="regionManager">
        /// The region Manager.
        /// </param>
        /// <param name="studiesService">
        /// The studies service.
        /// </param>
        /// <param name="subjectGroupAddView">
        /// The subject Group Add View.
        /// </param>
        /// <param name="subjectGroupEditView">
        /// The subject Group Edit View.
        /// </param>
        public SubjectGroupsViewModel(IRegionManager regionManager, IStudiesUIProxyService studiesService, ISubjectGroupAddView subjectGroupAddView, ISubjectGroupEditView subjectGroupEditView)
        {
            this.StudiesServiceInstance = studiesService;
            this.regionManagerInstance = regionManager;

            // Child Views: Add
            this.subjectGroupAddViewInstance = subjectGroupAddView;
            this.regionManagerInstance.Regions["MainRegion"].Add(subjectGroupAddView);

            this.subjectGroupEditViewInstance = subjectGroupEditView;
            this.regionManagerInstance.Regions["MainRegion"].Add(subjectGroupEditView);


            // Wire up the exit event for child views)
            this.subjectGroupAddViewInstance.ViewExit += this.ChildViewExit;
            this.subjectGroupEditViewInstance.ViewExit += this.ChildViewExit;

            // Setting up command delegates 
            this.EditSubjectGroup = new DelegateCommand<SubjectGroup>(this.OnEditSubjectGroup);
            this.AddSubjectGroup = new DelegateCommand(this.OnAddSubjectGroup);
            this.RemoveSubjectGroup = new DelegateCommand<SubjectGroup>(this.OnRemoveSubjectGroup);
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the add subjectGroup command.
        /// </summary>
        public ICommand AddSubjectGroup { get; private set; }

        /// <summary>
        /// Gets the edit subjectGroup command.
        /// </summary>
        public ICommand EditSubjectGroup { get; private set; }

        /// <summary>
        /// Gets the remove subjectGroup command.
        /// </summary>
        public ICommand RemoveSubjectGroup { get; private set; }

        /// <summary>
        /// Gets the studies.
        /// </summary>
        public ObservableCollection<SubjectGroup> SubjectGroups
        {
            get
            {
                return this.StudiesServiceInstance.GetAllSubjectGroups();
            }
        }

        /// <summary>
        /// Gets or sets View.
        /// </summary>
        public ISubjectGroupsView View { get; set; }

        /// <summary>
        /// Gets A studies service instance
        /// </summary>
        protected IStudiesUIProxyService StudiesServiceInstance { get; private set; }

        #endregion

        #region Implemented Interfaces

        #region ISubjectGroupsViewModel

        /// <summary>
        /// The initialize.
        /// </summary>
        public void Initialize()
        {
            // This is only here for uniformity and future use. Nothing special to initialize on the summary form.
        }

        /// <summary>
        /// Called when [add subjectGroup].
        /// </summary>
        public void OnAddSubjectGroup()
        {
            this.regionManagerInstance.Regions["MainRegion"].Activate(this.subjectGroupAddViewInstance);
        }

        /// <summary>
        /// Called when [edit subjectGroup].
        /// </summary>
        /// <param name="subjectGroup">
        /// The subjectGroup.
        /// </param>
        public void OnEditSubjectGroup(SubjectGroup subjectGroup)
        {
            if (subjectGroup == null)
            {
                return;
            }

            this.regionManagerInstance.Regions["MainRegion"].Activate(this.subjectGroupEditViewInstance);
        }

        /// <summary>
        /// Called when [remove subjectGroup].
        /// </summary>
        /// <param name="subjectGroup">
        /// The subjectGroup.
        /// </param>
        public void OnRemoveSubjectGroup(SubjectGroup subjectGroup)
        {
            if (subjectGroup == null)
            {
                return;
            }

            this.StudiesServiceInstance.RemoveSubjectGroup(subjectGroup);
            this.RaiseOnPropertyChanged("SubjectGroups");
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// Raises the on property changed.
        /// </summary>
        /// <param name="propertyName">
        /// Name of the property.
        /// </param>
        protected void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// The child view exit.
        /// </summary>
        private void ChildViewExit()
        {
            this.regionManagerInstance.Regions["MainRegion"].Activate(this.View);
            this.RaiseOnPropertyChanged("SubjectGroups");
        }

        #endregion
    }
}
